#include "myanimation.h"
#include "hqlabel.h"

MyAnimationScale::MyAnimationScale(QObject *target, const QByteArray &propertyName, QObject *parent) : QPropertyAnimation(target, propertyName, parent)
{
    m_current_time = 0;
    m_frame_num = 10;
    // 这样写不启动，是因为没有在类中写Q_OBJECT宏
    //connect(&mProgressTimer, SIGNAL(timeout()), this, SLOT(UpdateProgressTime()));

    //不加Q_OBJECT宏也可以实现定时器
    connect(&m_progress_timer, &QTimer::timeout, this, &MyAnimationScale::UpdateProgressTime);
}
MyAnimationScale::~MyAnimationScale()
{

}

void MyAnimationScale::Stop()
{
    if (m_progress_timer.isActive())
        m_progress_timer.stop();
    disconnect(&m_progress_timer, 0, this, 0);
    setCurrentTime(0);
}

void MyAnimationScale::SetStartValue(QVariant value)
{
    m_start_value = value;
    setStartValue(value);
}

void MyAnimationScale::SetEndValue(QVariant value)
{
    m_end_value = value;
    setEndValue(value);
}

void MyAnimationScale::SetDuration(int duration)
{
    m_duration = duration;
    setDuration(duration);
}

void MyAnimationScale::SetFrameNum(int num)
{
    m_frame_num = num;
}

void MyAnimationScale::Start()
{
    start(QAbstractAnimation::DeleteWhenStopped);
    pause();
    m_progress_timer.start(m_duration/m_frame_num);
}

void MyAnimationScale::UpdateProgressTime()
{
    //整个动画分为 默认10帧，
    m_current_time += m_duration/m_frame_num;
    //LOG_QDEBUG << m_current_time;

    //先启动再暂停，再根据自定义时间间隔设置帧(若不启动则下面函数不生效)
    setCurrentTime(m_current_time);

    if (m_current_time == m_duration)
        m_current_time = 0;
}


//动画类
MyAnimation::MyAnimation(HqLabel *target) : QPropertyAnimation(target, NULL)
{
    m_target = target;
    m_scale_animation = NULL;
}

MyAnimation::~MyAnimation()
{
    if (m_scale_animation != NULL)
    {
        delete m_scale_animation;
        m_scale_animation = NULL;
    }
    else
    {
        //结束常规动画除了scale和twoframescale
        stop();
    }
}

void MyAnimation::RunOpacityEffectAnimation()
{
    //外部实现
    //QPropertyAnimation *animation = new QPropertyAnimation(this);

    //此处和别的动画不同只要将label设置成父窗口即可，target和property在下面设置
    setParent(m_target);

    QGraphicsOpacityEffect *opacityEffect = new QGraphicsOpacityEffect;
    opacityEffect->setOpacity(1);
    m_target->setGraphicsEffect(opacityEffect);

    setDuration(3000);
    setTargetObject(opacityEffect);
    setPropertyName("opacity");
    /* 设置动画开始的不透明度 */
    setStartValue(0);
    /* 设置动画结束的不透明度 */
    setEndValue(1);
    /* 设置循环次数：-1为无限次 */
    setLoopCount(-1);
    start(QAbstractAnimation::DeleteWhenStopped);
}

void MyAnimation::RunLeftRightAnimation()
{
    setPropertyName("geometry");
    setDuration(3000);
    setStartValue(QRect(-m_target->width()/4, 0, m_target->width(), m_target->height()));
    setEndValue(QRect(m_target->width()/4, 0, m_target->width(), m_target->height()));
    setLoopCount(-1);
    start(QAbstractAnimation::DeleteWhenStopped);
}

void MyAnimation::Run2FrameScaleAnimation()
{
    m_target->setMinimumSize(0,0);
    setPropertyName("geometry");

    setDuration(1000);
    setStartValue(QRect(0, 0, m_target->width(), m_target->height()));
    setKeyValueAt(0.499, QRect(0, 0, m_target->width(), m_target->height()));
    setKeyValueAt(0.500, QRect(m_target->width()/10, m_target->height()/10, m_target->width()*4/5, m_target->height()*4/5));
    setKeyValueAt(0.999, QRect(m_target->width()/10, m_target->height()/10, m_target->width()*4/5, m_target->height()*4/5));
    setEndValue(QRect(0, 0, m_target->width(), m_target->height()));

    setLoopCount(-1);
    start(QAbstractAnimation::DeleteWhenStopped);
}

void MyAnimation::UpdateCurrentState(QAbstractAnimation::State state)
{
    //LOG_QDEBUG << "updateCurrentState  " << state;
}

void MyAnimation::UpdateCurrentValue(const QVariant &value)
{
    //LOG_QDEBUG << "updateCurrentValue  " << value;
    //QRect tmp = value.value<QRect>();
    //LOG_QDEBUG << tmp.m_target->width() << " -------- " << tmp.m_target->height();
}
//此动画需要重新实现所以继承了QPropertyAnimation重写了一些方法
void MyAnimation::RunScaleAnimation()
{
    m_target->setMinimumSize(0,0);

    m_scale_animation = new MyAnimationScale(m_target, "geometry");

    m_scale_animation->SetDuration(1000);
    m_scale_animation->SetStartValue(QRect(0, 0, m_target->width(), m_target->height()));

    //间隔设置为1/10   若间隔小于5则用5，大于5则使用最接近的5的倍数，因为默认10帧，放大缩小各5帧，所以间隔确保为5的倍数保证平缓渐变，能除尽
    int interval_x = m_target->width()/10 < 5 ? 5 : m_target->width()/10/5*5;
    int interval_y = m_target->height()/10 < 5 ? 5 : m_target->height()/10/5*5;
    m_scale_animation->setKeyValueAt(0.5, QRect(interval_x, interval_y, m_target->width()-interval_x*2, m_target->height()-interval_y*2));

    m_scale_animation->SetEndValue(QRect(0, 0, m_target->width(), m_target->height()));
    m_scale_animation->setLoopCount(-1);
    //可检测帧变化和动画状态
//    connect(animation, QPropertyAnimation::valueChanged, this, updateCurrentValue);
//    connect(animation, QPropertyAnimation::stateChanged, this, updateCurrentState);

    m_scale_animation->Start();
}

void MyAnimation::ReRunHqtRotateGrabAnimation(Animation animation)
{
    m_target->SetGrabPixmap();
    if (animation == Scale)
    {
        RunScaleAnimation();

    } else if (animation == TwoFrameScale)
    {
        Run2FrameScaleAnimation();
    }
}

void MyAnimation::RunHqtAnimation(QString place_way, Animation animation)
{
    if (animation == LeftRight)
    {
        RunLeftRightAnimation();

    } else if (animation == OpacityEffect)
    {
        RunOpacityEffectAnimation();

    } else if (animation == Scale)
    {
        if (place_way.contains("P") && place_way.size() > 1)
        {
            int digree = place_way.right(place_way.size() - 1).toInt();
            if (digree < 90 && digree > -90 && digree != 0)
            {
                m_target->SetGrabFlag(Scale);
                return;
            }
        }

        m_target->SetGrabPixmap();
        RunScaleAnimation();
        //此处本应该进行截屏之后再渲染png，放到show函数之后(因为show之前grab截屏不对)，先给设置一个标志位
    } else if (animation == TwoFrameScale)
    {
        if (place_way.contains("P") && place_way.size() > 1)
        {
            int digree = place_way.right(place_way.size() - 1).toInt();
            if (digree < 90 && digree > -90 && digree != 0)
            {
                m_target->SetGrabFlag(TwoFrameScale);
                return;
            }
        }
        m_target->SetGrabPixmap();
        Run2FrameScaleAnimation();
    }
}

void MyAnimation::RestoreAnimation()
{
    if (m_scale_animation != NULL)
    {
        m_scale_animation->Stop();
        //标志位变成无动画否则show之后会根据标志位运行动画
        m_target->SetGrabFlag(None);
    }
}

void MyAnimation::RunPngAnimation(Animation animation)
{
    if (animation == LeftRight)
    {
        RunLeftRightAnimation();

    } else if (animation == OpacityEffect)
    {
        RunOpacityEffectAnimation();

    } else if (animation == Scale)
    {
        RunScaleAnimation();

    } else if (animation == TwoFrameScale)
    {
        Run2FrameScaleAnimation();
    }
}
